<?php
namespace Model;
class Taobao
{
    //2016-03-15
    private $result;
	private $clientID = '23216166';
	private $clientSecret = '899a75ce3ae4afe550fd5c8ec3f3a627';
	private $sessionKey = '6202103c661ZZf4e194ebcd0567c08908ae68429b1d42aa2373557659';
	
	//groupName == tripvip
	
	public function __construct()
	{
	    Vendor('TaobaoSDK.TopSdk');
	    header("Content-Type: text/html; charset=UTF-8");
	    $this->result = array();
	}
	
	//绑定用户推送权限
    public function bindUserTopicsPermit($permit)
    {
        $c = new \TopClient;
		$c->appkey = $this->clientID;
		$c->secretKey = $this->clientSecret;
		
        $req = new \TmcUserPermitRequest;
        $req->setTopics($permit);
        $xml = $c->execute($req, $this->sessionKey);
        
        $data = json_decode(json_encode($xml),true);
        
        if(array_key_exists('code',$data) == true)
        {
            $this->result['valid'] = false;
            $this->result['msg'] = $data['msg'];
        }
        else
        {
            $this->result['valid'] = true;
            $this->result['msg'] = $data;
        }
        
        return $this->result;
    }
    
    //获取用户推送权限
    public function getUserTopicsPermit($nikeName)
    {
        $c = new \TopClient;
        $c->appkey = $this->clientID;
        $c->secretKey = $this->clientSecret;
    
        $req = new \TmcUserGetRequest;
        $req->setFields("user_nick,topics,user_id,is_valid,created,modified");
        $req->setNick($nikeName);
        $req->setUserPlatform("tbUIC");
        $xml = $c->execute($req, $this->sessionKey);
    
        $data = json_decode(json_encode($xml),true);
    
        if(array_key_exists('code',$data) == true)
        {
            $this->result['valid'] = false;
            $this->result['msg'] = $data['msg'];
        }
        else
        {
            $this->result['valid'] = true;
            $this->result['msg'] = $data;
        }
    
        return $this->result;
    }
    
    //设置用户推送信息分组
    public function setUserTopicsGroup($groupName,$userName)
    {
        $c = new \TopClient;
        $c->appkey = $this->clientID;
        $c->secretKey = $this->clientSecret;
    
        $req = new \TmcGroupAddRequest;
        $req->setGroupName($groupName);
        $req->setNicks($userName);
        $req->setUserPlatform("tbUIC");
        $xml = $c->execute($req, $this->sessionKey);
    
        $data = json_decode(json_encode($xml),true);
    
        if(array_key_exists('code',$data) == true)
        {
            $this->result['valid'] = false;
            $this->result['msg'] = $data['msg'];
        }
        else
        {
            $this->result['valid'] = true;
            $this->result['msg'] = $data;
        }
    
        return $this->result;
    }
    
    //获取用户推送信息分组
    public function getUserTopicsGroup($groupName)
    {
        $c = new \TopClient;
        $c->appkey = $this->clientID;
        $c->secretKey = $this->clientSecret;
    
        $req = new \TmcGroupsGetRequest;
        $req->setGroupNames($groupName);
        $req->setPageNo("1");
        $req->setPageSize("40");

        $xml = $c->execute($req, $this->sessionKey);
    
        $data = json_decode(json_encode($xml),true);
    
        if(array_key_exists('code',$data) == true)
        {
            $this->result['valid'] = false;
            $this->result['msg'] = $data['msg'];
        }
        else
        {
            $this->result['valid'] = true;
            $this->result['msg'] = $data;
        }
    
        return $this->result;
    }
    
    //获取推送信息
    public function getTopicsConsume()
    {
        $c = new \TopClient;
        $c->appkey = $this->clientID;
        $c->secretKey = $this->clientSecret;
    
        $req = new \TmcMessagesConsumeRequest;
        $req->setGroupName('tripvip');
        $req->setQuantity(100);
    
        $xml = $c->execute($req, $this->sessionKey);

        $data = json_decode(json_encode($xml),true);
    
        if(array_key_exists('code',$data) == true)
        {
            $this->result['valid'] = false;
            $this->result['msg'] = $data['msg'];
        }
        else
        {
            $result = array();
            
            foreach ($data['messages']['tmc_message'] as $key => $value)
            {
                if(is_array($value) == true)
                {
                    $result[] = $value;
                }
            }
            
            if(empty($result) == true && empty($data['messages']['tmc_message']) == false)
            {
                $result[] = $data['messages']['tmc_message'];
            }
            
//             foreach ($data as $key => $value)
//             {
//                 //Array ( [content] => {"buyer_nick":"心点虫","payment":"494.00","oid":1403348967131423,"tid":1403348967131423,"type":"guarantee_trade","seller_nick":"广东粤侨国旅专营店"} [id] => 2112101110478557626 [pub_app_key] => 12497914 [pub_time] => 2015-11-27 19:21:15 [topic] => taobao_trade_TradeSuccess [user_id] => 2373557659 [user_nick] => 广东粤侨国旅专营店 )
//                 switch ($value['topic'])
//                 {
//                     //订单号：$value['tid']
//                     //支付成功
//                     case 'taobao_fuwu_OrderPaid':
            
//                         break;
//                         //交易成功
//                     case 'taobao_trade_TradeSuccess':
            
//                         break;
//                         //订单退款
//                     case 'taobao_refund_RefundSuccess':
            
//                         break;
//                         //创建订单
//                     case 'taobao_fuwu_OrderCreated':
            
//                         break;
//                 }
//                 //$this->confirmTopicsConsume($value['id']);
//             }

            $this->result['valid'] = true;
            $this->result['msg'] = $result;
        }
    
        return $this->result;
    }
    
    //确认推送信息
    public function confirmTopicsConsume($messageID)
    {
        $c = new \TopClient;
        $c->appkey = $this->clientID;
        $c->secretKey = $this->clientSecret;
    
        $req = new \TmcMessagesConfirmRequest;
        $req->setGroupName("tripvip");
        $req->setSMessageIds($messageID);

        $xml = $c->execute($req, $this->sessionKey);
    
        $data = json_decode(json_encode($xml),true);
    
        if(array_key_exists('code',$data) == true)
        {
            $this->result['valid'] = false;
            $this->result['msg'] = $data['msg'];
        }
        else
        {
            $this->result['valid'] = true;
            $this->result['msg'] = $data;
        }
    
        return $this->result;
    }

    public function getOrderList($startTime,$endTime)
    {
        $result = array();
        $page = 1;
        do{
            $return = $this->orderListRecursion($startTime,$endTime,$page);
            if(empty($return) == false && isset($return['result']['created']) == true)
            {
                $result[] = $return['result'];
            }
            else
            {
                foreach ($return['result'] as $key => $value)
                {
                    $result[] = $value;
                }
            }
            $page++;
        }
        while($return['continue'] == true);
    
    
        return $result;
    }
    
    public function orderListRecursion($startTime,$endTime,$page)
    {
        $c = new \TopClient;
        $c->appkey = $this->clientID;
        $c->secretKey = $this->clientSecret;
    
        $req = new \TradesSoldGetRequest;
        $req->setFields("tid,type,status,payment,created,pay_time,receiver_name,receiver_mobile");
        $req->setStartCreated($startTime);
        $req->setEndCreated($endTime);
        $req->setPageNo($page);
        $req->setPageSize("40");
        $req->setUseHasNext("true");
        $xml = $c->execute($req, $this->sessionKey);
    
        $data = json_decode(json_encode($xml),true);

        $result = array();
        foreach ($data['trades']['trade'] as $key => $value)
        {
            $result[$key] = $value;
        }
    
        $continue = false;
    
        if($data['has_next'] == 'true')
        {
            $continue = true;
        }
    
        return array('continue'=>$continue,'result'=>$result);
    }
    
    public function getOrderIncrement($startTime,$endTime)
    {
        $result = array();
        $page = 1;
        do{
            $return = $this->orderIncrementRecursion($startTime,$endTime,$page);
            if(empty($return) == false && isset($return['result']['created']) == true)
            {
                $result[] = $return['result'];
            }
            else
            {
                foreach ($return['result'] as $key => $value)
                {
                    $result[] = $value;
                }
            }
            $page++;
        }
        while($return['continue'] == true);
        
        
        return $result;
    }
    
    public function orderIncrementRecursion($startTime,$endTime,$page)
    {
        $c = new \TopClient;
        $c->appkey = $this->clientID;
        $c->secretKey = $this->clientSecret;
    
        $req = new \TradesSoldIncrementGetRequest;
        $req->setFields("tid,type,status,payment,created,pay_time,receiver_name,receiver_mobile");
        $req->setStartModified($startTime);
        $req->setEndModified($endTime);
        $req->setPageNo($page);
        $req->setPageSize("50");
        $req->setUseHasNext("true");
        $xml = $c->execute($req, $this->sessionKey);
    
        $data = json_decode(json_encode($xml),true);
    
        $result = array();
        foreach ($data['trades']['trade'] as $key => $value)
        {
            $result[$key] = $value;
        }
    
        $continue = false;
    
        if($data['has_next'] == 'true')
        {
            $continue = true;
        }

        return array('continue'=>$continue,'result'=>$result);
    }
    
    public function getOrderDetail($orderID)
    {
        $c = new \TopClient;
        $c->appkey = $this->clientID;
        $c->secretKey = $this->clientSecret;
        
        $req = new \TradeFullinfoGetRequest;
        $req->setFields("seller_memo,tid,type,status,payment,orders,created,pay_time,receiver_name,receiver_mobile,buyer_alipay_no");
        $req->setTid($orderID);
        $xml = $c->execute($req, $this->sessionKey);
        
        $data = json_decode(json_encode($xml),true);
        
        if(array_key_exists('code',$data) == true)
        {
            $this->result['valid'] = false;
            $this->result['result'] = $data['sub_code'];
        }
        else
        {
            $data = $data['trade'];
            
            $result = array();
            $result = $data;
            unset($result['orders']);

            $resultTMP = array();
            foreach($data['orders']['order'] as $key => $value)
            {
                if(is_array($value) == true)
                {
                    $resultTMP[] = $value;
                }
            }
            
            if(empty($resultTMP) == true)
            {
                $resultTMP[] = $data['orders']['order'];
            }
            
            $result['list'] = $resultTMP;
   
            $this->result['valid']  = true;
            $this->result['result']    = $result;
        }
        
        return $this->result;
    }
    
    public function getOrderRefund($reundID)
    {
        $c = new \TopClient;
        $c->appkey = $this->clientID;
        $c->secretKey = $this->clientSecret;
    
        $req = new \RefundGetRequest;
        $req->setFields("created,refund_fee,reason,status,modified");
        $req->setRefundId($reundID);
        $xml = $c->execute($req, $this->sessionKey);
    
        $data = json_decode(json_encode($xml),true);
    
        if(array_key_exists('code',$data) == true)
        {
            $this->result['valid'] = false;
            $this->result['result'] = $data['msg'];
        }
        else
        {
            $result = $data['refund'];
            unset($result['operation_contraint']);
            $this->result['valid']  = true;
            $this->result['result'] = $result;
        }
    
        return $this->result;
    }
    
    //发货
	public function delivery($orderID,$expressSingle,$companyCode)
	{
	    $result = array('valid'=>true,'msg'=>'');
	    
		$c = new \TopClient;
		$c->appkey = $this->clientID;
		$c->secretKey = $this->clientSecret;
		
		$req = new \LogisticsOfflineSendRequest;
		$req->setIsSplit(0);
		$req->setTid($orderID);
		$req->setOutSid($expressSingle);
		$req->setCompanyCode($companyCode);
		$xml = $c->execute($req, $this->sessionKey);

		$data = json_decode(json_encode($xml),true);

		if(array_key_exists('code',$data) == true)
		{
			$this->result['valid'] = false;
			$this->result['msg'] = $data['msg'];
		}
		else
		{
		    $this->result['valid'] = true;
			$this->result['msg'] = '发货成功！';
		}

		return $this->result;
	}
	
}

?>